package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.entity.member.LiftRulesEntity;
import com.dragon.modules.yuegou.enums.code.BusinessCode;
import com.dragon.modules.yuegou.enums.code.MemberCode;
import com.dragon.modules.yuegou.enums.member.LiftRulesFormat;
import com.dragon.modules.yuegou.mapper.member.LiftRulesMapper;
import com.dragon.modules.yuegou.model.member.LiftRules;
import com.dragon.modules.yuegou.query.member.LiftRulesQuery;
import com.dragon.modules.yuegou.service.member.LiftRulesService;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 会员升降规则说明表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-03-19 08:59:40
 * @version 2.0
 */
@Service("memLiftRulesService")
public class LiftRulesServiceImpl
		extends ServiceImpl<LiftRulesMapper, LiftRulesEntity>
		implements LiftRulesService {

	public void saveLiftRules(LiftRules liftRules){
		Check.notNull(liftRules.getBusdId(), BusinessCode.BUSDID_NOTNULL.getMessage());
		Check.notNull(liftRules.getFormat(),"规则格式不能为空");
		if(liftRules.getFormat().getCode().equals(LiftRulesFormat.TEXT.getCode())) {
			Check.notEmpty(liftRules.getTitle(), "规则标题不能为空");
			Check.notEmpty(liftRules.getRemark(), "文字说明不能为空");
		}else{
			Check.notEmpty(liftRules.getImg(), "请上传图片");
		}
		if(StringUtils.isEmpty(liftRules.getId())){
			LiftRulesQuery query = new LiftRulesQuery();
			query.setBusdId(liftRules.getBusdId());
			query.setDel(false);
			Long count = count(query.toQueryWrapper());
			if(count > 0){
				throw new DragonException(MemberCode.MEMBER_LIFT_RULES);
			}
			save(liftRules);
		}else {
			baseMapper.updateById(liftRules);
		}
	}

}